<template>
    <div>
        <div class="con1">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item label="病历号">
                    <el-input v-model="caseNumber" placeholder="请输入病历号..."></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                </el-form-item>
            </el-form>
            <el-divider></el-divider>
        </div>
        <div class="con2" v-loading="loading">
            <el-table stripe
                      :data="tableData"
                      style="width: 100%">
                <el-table-column
                        fixed
                        prop="caseNumber"
                        label="病历号"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="realName"
                        label="姓名"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="age"
                        label="年龄"
                        width="180"
                        :formatter="formatter2">
                </el-table-column>
                <el-table-column
                        prop="gender"
                        label="性别"
                        width="180"
                        :formatter="formatter3">
                </el-table-column>
                <el-table-column
                        prop="registLeID"
                        label="挂号级别"
                        width="180">
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.registLeID == '1' ? 'primary' : 'danger'"
                                size="small" plain>
                            {{scope.row.registLeID==1?'普通':'专家'}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="registTime"
                        label="挂号日期"
                        width="180"
                        :formatter="formatter">
                </el-table-column>
                <el-table-column
                        prop="homeAddress"
                        label="地址"
                        width="180">
                    <template slot-scope="scope">
                        <el-popover trigger="hover" placement="top">
                            <p>{{ scope.row.homeAddress }}</p>
                            <div slot="reference" class="name-wrapper intro">
                                <el-tag>{{ scope.row.homeAddress }}</el-tag>
                            </div>
                        </el-popover>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="visitState"
                        label="看诊状态"
                        width="180"
                >
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.visitState == '4' ? 'danger' : 'primary'"
                                size="small" plain>
                            {{scope.row.visitState==1?'已挂号':scope.row.visitState==2?'正接诊':scope.row.visitState==3?'已看诊':scope.row.visitState==4?'已退号':'已失效'}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="200">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="update(scope.row)" icon="el-icon-edit" circle></el-button>
                        <el-button type="danger" @click="del(scope.row)" icon="el-icon-delete" circle></el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page.sync="currentPage1"
                    :page-size="pagesize1"
                    layout="total, prev, pager, next,jumper"
                    :total="totalNum">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                loading:true,
                pagesize1: 5,
                totalNum: 0,
                currentPage1: 1,
                caseNumber: '',
                tableData: []
            }
        },
        mounted() {
            this.$ajax.get("http://localhost:8082/queryAll/1").then((res) => {
                this.tableData = res.data.list
                this.totalNum = res.data.total
                this.loading=false
            })
        },
        methods: {
            del(row){
                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$ajax.delete(`http://localhost:8082/delOne/${row.ID}`).then((res) => {
                        if(res.data=="success"){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.$ajax.get(`http://localhost:8082/queryAll/${this.currentPage1}`).then((res) => {
                                this.tableData = res.data.list
                                this.totalNum = res.data.total
                            })
                        }else{
                            this.$message({
                                type: 'danger',
                                message: '删除失败!'
                            });
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            update(row){
                this.$router.push(`/register_update/${row.ID}`)
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                this.$ajax.get(`http://localhost:8082/queryAll/${val}`).then((res) => {
                    this.tableData = res.data.list
                })
            },
            onSubmit() {
                this.$ajax.get(`http://localhost:8082/queryOne?caseNumber=${this.caseNumber}`).then((res) => {
                    this.tableData = res.data
                })
            },
            formatter3(row, column) {
                let sex = row.gender;
                if (sex == 71) return '男'
                else
                    return '女';
            },
            formatter2(row, column) {
                let age = row.age + row.ageType;
                return age;
            },
            formatter(row, column) {
                let d = new Date(row[column.property]);
                let year = d.getFullYear();
                let month = d.getMonth();
                let day = d.getDate();
                let hour = d.getHours();
                let minute = d.getMinutes();
                let s = year + "-" + this.format(month + 1) + "-" + this.format(day) + " " + this.format(hour) + ":" + this.format(minute)
                return s
            },
            format(d) {
                return d > 9 ? d : '0' + d;
            },
        }
    }
</script>

<style scoped>
    .con1 {
        text-align: center;
    }
    .intro {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
</style>